home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / pgp20src.zip / GETOPT.C < prev    next >
C/C++ Source or Header  |  1992-08-05  |  2KB  |  87 lines

  1. /*
  2. **    @(#)getopt.c    2.5 (smail) 9/15/87
  3. */
  4.  
  5. /*
  6. *  This is the AT&T public domain source for getopt(3).  It is the code
  7. *  which was given out at the 1985 UNIFORUM conference in Dallas.
  8. *   
  9. *  There is no manual page.  That is because the one they gave out at
  10. *  UNIFORUM was slightly different from the current System V Release 2
  11. *  manual page.  The difference apparently involved a note about the
  12. *  famous rules 5 and 6, recommending using white space between an
  13. *  option and its first argument, and not grouping options that have
  14. *  arguments.  Getopt itself is currently lenient about both of these
  15. *  things.  White space is allowed, but not mandatory, and the last option
  16. *  in a group can have an argument.  That particular version of the man
  17. *  page evidently has no official existence.  The current SVR2 man page
  18. *  reflects the actual behavor of this getopt.
  19. */
  20.  
  21. #include <string.h>
  22.  
  23. /*LINTLIBRARY*/
  24. #ifndef NULL
  25. #define NULL    0
  26. #endif
  27. #define EOF    (-1)
  28. #define ERR(s, c)    if(opterr){\
  29.     extern int write();\
  30.     char errbuf[2];\
  31.     errbuf[0] = c; errbuf[1] = '\n';\
  32.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  33.     (void) write(2, s, (unsigned)strlen(s));\
  34.     (void) write(2, errbuf, 2);}
  35.  
  36. int    opterr = 1;
  37. int    optind = 1;
  38. int    optopt;
  39. char    *optarg;
  40.  
  41. int
  42. getopt(argc, argv, opts)
  43. int    argc;
  44. char    **argv, *opts;
  45. {
  46.     static int sp = 1;
  47.     register int c;
  48.     register char *cp;
  49.  
  50.     if(sp == 1)
  51.         if(optind >= argc ||
  52.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  53.             return(EOF);
  54.         else if(strcmp(argv[optind], "--") == 0) {
  55.             optind++;
  56.             return(EOF);
  57.         }
  58.     optopt = c = argv[optind][sp];
  59.     if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  60.         ERR(": illegal option -- ", c);
  61.         if(argv[optind][++sp] == '\0') {
  62.             optind++;
  63.             sp = 1;
  64.         }
  65.         return('\0');
  66.     }
  67.     if(*++cp == ':') {
  68.         if(argv[optind][sp+1] != '\0')
  69.             optarg = &argv[optind++][sp+1];
  70.         else if(++optind >= argc) {
  71.             ERR(": option requires an argument -- ", c);
  72.             sp = 1;
  73.             return('\0');
  74.         } else
  75.             optarg = argv[optind++];
  76.         sp = 1;
  77.     } else {
  78.         if(argv[optind][++sp] == '\0') {
  79.             sp = 1;
  80.             optind++;
  81.         }
  82.         optarg = NULL;
  83.     }
  84.     return(c);
  85. }
  86.  
  87.